package com.yanqu.road.server.cmd.wingroom.shareevent;

import com.yanqu.road.entity.player.ReturnData;
import com.yanqu.road.entity.wingroom.UserWingRoomShareEvent;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.wingroom.WingRoomMgr;
import com.yanqu.road.server.pb.WingRoomPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

/**
 * @author ：zfr
 * @date ：2022/2/11 10:06
 */
@Cmd(code = GameProtocol.S_WING_ROOM_GET_SELF_EVENT_REWARD, desc = "领取自己厢房事件的奖励")
public class GetSelfEventRewardCmd extends AbstractCommand {
    private int pos;
    private ReturnData returnData;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        WingRoomProto.GetSelfEventRewardReqMsg reqMsg = WingRoomProto.GetSelfEventRewardReqMsg.parseFrom(packet.getBytes());
        pos = reqMsg.getPos();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        returnData = WingRoomMgr.getSelfEventReward(pos, player);
    }

    @Override
    public void sendData(GamePlayer player) {
        WingRoomProto.GetSelfEventRewardRespMsg.Builder respMsg = WingRoomProto.GetSelfEventRewardRespMsg.newBuilder();
        respMsg.setRet(returnData.getRet());
        if (returnData.getRet() == 0) {
            respMsg.setReward(returnData.getParams().get(0));
        }
        UserWingRoomShareEvent userEvent = WingRoomMgr.getUserEvent(player.getUserId());
        if (userEvent != null) {
            respMsg.setEvent(WingRoomPb.buildEventTempMsg(userEvent));
        }
        player.sendPacket(ClientProtocol.U_WING_ROOM_GET_SELF_EVENT_REWARD, respMsg);
    }
}
